package chenji.itemeditor.itemeditor;

import java.util.HashSet;

public class HideFlagsSet {
	private HashSet<HideFlag> flag = new HashSet<>();

	public HideFlagsSet() {
	}

	public HideFlagsSet(int value) {
		this.loadFromTag(value);
	}

	public void add(HideFlag f) {
		if (this.flag.contains(f)) {
			return;
		}
		this.flag.add(f);
	}
	public void remove(HideFlag f){
		this.flag.remove(f);
	}

	public boolean contain(HideFlag f) {
		return this.flag.contains(f);
	}

	public int getValue() {
		int value = 0;
		for (HideFlag f : this.flag) {
			value = value + f.getID();
		}
		return value;
	}

	private HideFlag getMaxFlag(int flag) {
		if (flag >= 32) {
			return HideFlag.OTHERS;
		}
		if (16 <= flag && flag < 32) {
			return HideFlag.CANPLACEBLOCK;
		}
		if (8 <= flag && flag < 16) {
			return HideFlag.CANBREAKBLOCK;
		}
		if (4 <= flag && flag < 8) {
			return HideFlag.UNBREAKABLE;
		}
		if (2 <= flag && flag < 4) {
			return HideFlag.ATTRBUTE;
		}
		if (flag == 1) {
			return HideFlag.ENCH;
		}
		return null;
	}

	private void loadFromTag(int tag) {
		while (tag != 0) {
			HideFlag f = this.getMaxFlag(tag);
			if (f == null) {
				break;
			}
			tag = tag - f.getID();
			this.flag.add(f);
		}

	}
}
